// https://api.matsurihi.me/mltd/v1/cards


let nameListDom = document.createElement("div");
nameListDom.style.display = "none";

let cardUrlListDom = document.createElement("div");
cardUrlListDom.style.display = "none";

let bodyDom = document.getElementsByTagName("body")[0];
bodyDom.appendChild(nameListDom);
bodyDom.appendChild(cardUrlListDom);

let xml = new XMLHttpRequest();
xml.open("GET", "https://api.matsurihi.me/mltd/v1/cards", true);

let rarityList = ["NULL", "N", "R", "SR", "SSR"];

xml.onreadystatechange = function() {
    if (xml.readyState === 4 && xml.status === 200) {
        let data = JSON.parse(xml.responseText);
        let CARD_BASE_URL = "https://storage.matsurihi.me/mltd/card/";
        let BG_BASE_URL = "https://storage.matsurihi.me/mltd/card_bg/";

        let nameList = "";
        let cardUrlList = "";

        for (let i = 1200; i < data.length; i++) {
            let cardInfo = data[i];

            let idolName0 = cardInfo["name"];
            let idolNameBlankIndex = idolName0.lastIndexOf('　');

            let idolName = "";
            let idolPreName = "NULL";
            if (idolNameBlankIndex >= 0) {
                idolName = idolName0.substring(idolNameBlankIndex + 1);
                idolPreName = idolName0.substring(0, idolNameBlankIndex).replaceAll("-", "_");
            } else {
                idolName = idolName0;
            }

            let rarity = rarityList[cardInfo["rarity"]];

            let skillName = cardInfo["skillName"];
            if (skillName === "---") {
                skillName = "NULL";
            }
            skillName = skillName.replaceAll("-", "_");

            let resourceId = cardInfo["resourceId"];

            let cardUrl = CARD_BASE_URL + resourceId + "_0_b.png";
            let cardIdolizedUrl = CARD_BASE_URL + resourceId + "_1_b.png";
            cardUrlList += cardUrl + "<br />" + cardIdolizedUrl + "<br />";

            // 资源id - 系列名 - 普通/特训（空缺） - 等级（n/r/sr/ssr） - 名字 - 技能吗 - 卡id
            // 资源id - 卡id - 名字 - 系列名 - 等级（n/r/sr/ssr） - 普通/特训（空缺）- 技能
            let cardName = resourceId + "_0_b~~~MLTD卡牌-" + cardInfo["id"] + "-" + idolName + "-" + idolPreName + "-" + rarity + "-普通-" + skillName;
            let cardIdolizedName = resourceId + "_1_b~~~MLTD卡牌-" + cardInfo["id"] + "-" + idolName + "-" + idolPreName + "-" + rarity + "-特训-" + skillName;
            nameList += cardName + "<br />" + cardIdolizedName + "<br />";
            console.log(cardName);
            console.log(cardIdolizedName);

            if (cardInfo["rarity"] === 4) {
                let bgUrl = BG_BASE_URL + resourceId + "_0.png";
                let bgIdolizedUrl = BG_BASE_URL + resourceId + "_1.png";
                cardUrlList += bgUrl + "<br />" + bgIdolizedUrl + "<br />";

                let bgName = resourceId + "_0~~~MLTD壁纸-" + cardInfo["id"] + "-" + idolName + "-" + idolPreName + "-" + rarity + "-普通-" + skillName;
                let bgIdolizedName = resourceId + "_1~~~MLTD壁纸-" + cardInfo["id"] + "-" + idolName + "-" + idolPreName + "-" + rarity + "-特训-" + skillName
                nameList += bgName + "<br />" + bgIdolizedName + "<br />";
                console.log(bgName);
                console.log(bgIdolizedName);
            }


        }
        nameListDom.innerHTML = nameList;
        cardUrlListDom.innerHTML = cardUrlList;
        console.log("end! ")
    }
}
xml.send();